package com.ycsj.goldmedalnewconcept.activity;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.google.gson.Gson;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.picasso.Picasso;
import com.ycsj.goldmedalnewconcept.bean.ScoreDetailResponse;
import com.ycsj.goldmedalnewconcept.bean.ScoreDetailResponse.ScoreDetail;
import com.ycsj.goldmedalnewconcept.constant.Constant;
import com.ycsj.goldmedalnewconcept.utils.SPUtil;
import com.ycsj.goldmedalnewconcept.R;

//学生成绩详情（教师端）
public class ScoreDetailActivity extends Activity implements OnClickListener {

	private ImageView iv_back;
	private Intent intent;
	private ListView lv_gonggao;
	private int position;
	private String role;
	private String account;
	private String tagid;
	private String userclass;
	private String userid;
	private String name;
	private GongaoInfoAdapter mAdapter;
	private List<ScoreDetail> scorelist;
	private List<String> imglist;
	private TextView tv_score;
	private TextView title;
	private TextView tv_time;
	private TextView tv_content;

	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_scoredetail); 
		role = SPUtil.getString(this, "role", "");
		account = SPUtil.getString(this, "account", "");

		intent = getIntent();
		name = intent.getStringExtra("name");
		tagid = intent.getStringExtra("tagid");
		userclass = intent.getStringExtra("userclass");
		userid = intent.getStringExtra("userid");

		Log.e("testt", "name"+name);
		Log.e("testt", "tagid"+tagid);
		Log.e("testt", "userid"+userid);
		Log.e("testt", "userclass"+userclass);
		
		initView();
		setOnListener();
		initData();
	}

	private void initView() {
		// TODO Auto-generated method stub
		iv_back = (ImageView) findViewById(R.id.iv_back);
		lv_gonggao = (ListView) findViewById(R.id.lv_gonggao);
		title = (TextView) findViewById(R.id.title);
		
		if(name!=null&&!"".equals(name)){
			title.setText(name+"的成绩");
		}
		
		
		View headerView = LayoutInflater.from(getBaseContext()).inflate(
				R.layout.scoredetail_header, null);
		tv_score = (TextView) headerView.findViewById(R.id.tv_score);
		tv_content = (TextView) headerView.findViewById(R.id.tv_content);

		lv_gonggao.addHeaderView(headerView);
		
		// View view =
		// lv_gonggao.addHeaderView();

		// lv_gonggao.setAdapter(adapter);
	}

	private void initData() {

		OkHttpClient mOkHttpClient = new OkHttpClient();
		// mOkHttpClient.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
		FormEncodingBuilder builder = new FormEncodingBuilder();
		builder.add("type", "39");
		builder.add("c", userid);
		builder.add("role", "0");
		builder.add("userclass", userclass);
		builder.add("teachermobile", account);
		builder.add("tagid", tagid);
		Request request = new Request.Builder()
				.url(Constant.SERVER_LOC + "ManageMsg").post(builder.build())
				.build();
		mOkHttpClient.newCall(request).enqueue(new Callback() {

// http://192.168.0.198:8888/ycsj_platform/ManageMsg?type=39&c=18271261779&role=0&userclass=2722&teachermobile=13006314760&tagid=22
			@Override
			public void onFailure(Request arg0, IOException arg1) {
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						Toast.makeText(getBaseContext(), "网络无响应，请检查网络连接", 0)
								.show();
					}
				});
			}

			@Override
			public void onResponse(Response arg0) throws IOException {
				// 解析Json数据，保存登录状态及身份信息

				Gson gson = new Gson();
				String json = arg0.body().string();
				Log.e("test", json);
				ScoreDetailResponse fromJson = gson.fromJson(json,
						ScoreDetailResponse.class);
 
				if (fromJson != null) {
					if ("-1".equals(fromJson.state)
							|| "0".equals(fromJson.state)) {
						runOnUiThread(new Runnable() {
							@Override
							public void run() {
								// tv_videonull
								// .setVisibility(tv_videonull.VISIBLE);
							}
						});
					} else if ("1".equals(fromJson.state)) {
						scorelist = fromJson.list;

						if (position != -1) {
							imglist = new ArrayList<String>();
							if (scorelist != null) {
								if (scorelist.get(position).picture1 != null
										&& !"".equals(scorelist.get(position).picture1)) {
									imglist.add(scorelist.get(position).picture1);
								}
								if (scorelist.get(position).picture2 != null
										&& !"".equals(scorelist.get(position).picture2)) {
									imglist.add(scorelist.get(position).picture2);
								}
								if (scorelist.get(position).picture3 != null
										&& !"".equals(scorelist.get(position).picture3)) {
									imglist.add(scorelist.get(position).picture3);
								}
								if (scorelist.get(position).picture4 != null
										&& !"".equals(scorelist.get(position).picture4)) {
									imglist.add(scorelist.get(position).picture4);
								}
								if (scorelist.get(position).picture5 != null
										&& !"".equals(scorelist.get(position).picture5)) {
									imglist.add(scorelist.get(position).picture5);
								}
								if (scorelist.get(position).picture6 != null
										&& !"".equals(scorelist.get(position).picture6)) {
									imglist.add(scorelist.get(position).picture6);
								}
								if (scorelist.get(position).picture7 != null
										&& !"".equals(scorelist.get(position).picture7)) {
									imglist.add(scorelist.get(position).picture7);
								}
								if (scorelist.get(position).picture8 != null
										&& !"".equals(scorelist.get(position).picture8)) {
									imglist.add(scorelist.get(position).picture8);
								}
								if (scorelist.get(position).picture9 != null
										&& !"".equals(scorelist.get(position).picture9)) {
									imglist.add(scorelist.get(position).picture9);
								}
							}
						}
						mAdapter = new GongaoInfoAdapter();
						runOnUiThread(new Runnable() {
							@Override
							public void run() {
								tv_score.setText("测试分数："+scorelist.get(position).score);
								tv_content.setText(scorelist.get(position).content);

								lv_gonggao.setAdapter(mAdapter);
							}
						});
					}
				}
			}
		});

	}

	private void setOnListener() {
		// TODO Auto-generated method stub
		iv_back.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.iv_back:
			finish();
			break;

		}
	}

	class GongaoInfoAdapter extends BaseAdapter {

		public int getCount() {

			return imglist.size();
		}

		public Object getItem(int position) {

			return null;
		}

		public long getItemId(int position) {

			return 0;
		}

		ViewHolder vh = null;

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			if (convertView == null) {
				convertView = View.inflate(parent.getContext(),
						R.layout.item_gonggaodetail, null);
				vh = new ViewHolder();

				vh.iv_img = (ImageView) convertView.findViewById(R.id.iv_img);

				convertView.setTag(vh);
			} else {
				vh = (ViewHolder) convertView.getTag();
			}

			// if(gonggaolist.get(position).user_picture1!=null){
			//
			// Picasso.with(GonggaoActivity.this)
			// .load(gonggaolist.get(position).user_picture1).into(vh.iv_img);
			// }else{
			// vh.iv_img.setVisibility(vh.iv_img.GONE);
			// }
			Picasso.with(ScoreDetailActivity.this)
					.load(imglist.get(position)).into(vh.iv_img);

			return convertView;
		}

		class ViewHolder {
			private ImageView iv_img;

		}
	}

}
